package com.example.wangzh.developkit.utils;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.Log;

import com.example.wangzh.developkit.constant.LogTag;
import com.google.gson.Gson;

public class DeviceUtils {
    private static final String TAG = "DeviceUtils";

    /**
     * 系统版本号
     */
    public static String getSystemVersion() {
        return android.os.Build.VERSION.RELEASE;
    }

    /**
     * mac地址
     *
     * @param con
     * @return
     */
    public static String getLocalMacAddress(Context con) {
        WifiManager wifi = (WifiManager) con.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifi.getConnectionInfo();
        return info.getMacAddress();
    }

    /**
     * @param context
     * @return 检测是否有网络
     */
    public static boolean isNetwork(Context context) {
        boolean isConnected;
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getApplicationContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        Gson gson = new Gson();
        if (connectivityManager == null) {
            isConnected = false;
            LogUtils.i(LogTag.NET_INFO, "connectivityManager为空");
            LogUtils.i(LogTag.NET_INFO, "isConnected>>>" + isConnected);
            return isConnected;
        }
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if (networkInfo == null) {
            isConnected = false;
            LogUtils.i(LogTag.NET_INFO, "ActiveNetworkInfo为空");
            LogUtils.i(LogTag.NET_INFO, "isConnected>>>" + isConnected);
            return isConnected;
        }
        if (!networkInfo.isAvailable()) {
            isConnected = false;
            LogUtils.i(LogTag.NET_INFO, "networkInfo.isAvailable()>>>false>>>" + gson.toJson(networkInfo));
            LogUtils.i(LogTag.NET_INFO, "isConnected>>>" + isConnected);
            return isConnected;
        }
        if (null == networkInfo.getState()) {
            isConnected = false;
            LogUtils.i(LogTag.NET_INFO, "networkInfo.getState()为空>>>" + gson.toJson(networkInfo));
            LogUtils.i(LogTag.NET_INFO, "isConnected>>>" + isConnected);
            return isConnected;
        }
        if (State.CONNECTED != networkInfo.getState()) {
            isConnected = false;
            LogUtils.i(LogTag.NET_INFO, "networkInfo.getState()不为CONNECTED>>>" + gson.toJson(networkInfo));
            LogUtils.i(LogTag.NET_INFO, "isConnected>>>" + isConnected);
            return isConnected;
        }
        isConnected = true;
        LogUtils.i(LogTag.NET_INFO, "网络已连接》》》" + gson.toJson(networkInfo));
        LogUtils.i(LogTag.NET_INFO, "isConnected>>>" + isConnected);
        return isConnected;
    }

    /**
     * 获取设备的唯一标识，deviceId
     *
     * @param context
     * @return
     */
    public static String getDeviceId(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String deviceId = tm.getDeviceId();
        if (deviceId == null) {
            return null;
        } else {
            return deviceId;
        }
    }
}
